package com.jyh.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.Map;
import com.alibaba.fastjson.JSONObject;
import com.jyh.config.quantity.ConstantConfig;


public class NoteUtils {
	
	/**
	 * 发送短信模版替换
	 * @param test 短信模版
	 * @param map 替换文本
	 * @return
	 */
	public static String getNoteSMSTextReplace(String test,Map<String,Object> map){
		for (String key : map.keySet()) {
			test = test.replace("[" + key + "]", "[ " + map.get(key) + " ]");
		}
		return test;
	}
	
	/**
	 * 短信发送
	 */
	public static String getNoteSend(String mobile,String code){
		String token = getToken();
		return sendSms(mobile, code, token);
	}
	
	
	
	/**
	 * 获取短信收发  Token
	 * @return Token
	 */
	private static String getToken() {
		String res = sendPost(ConstantConfig.NOTEHTTPTOKENURL,"serviceName=GET_TOKEN&serviceSYS=jyh_loan");
		JSONObject jsonObj = JSONObject.parseObject(res.toString());
		System.out.println(jsonObj);
		JSONObject token = JSONObject.parseObject(jsonObj.get("data")+"");
		return token.get("token")+"";
	}


	/**
	 * 短信发送 Send-Sms
	 * @param dataz
	 * 0000:发送成功
       0001:json解析失败
       0002:未获取token值
       0003:token失效
       0004:发送失败，查询具体失败原因
       0005:来源信息未明确
	 */
	private static String sendSms(String data, String note, String token) {
		String JsonStr = "{\"data\":\""+data + "\",\"note\":\"" + note +"\",\"source\":\"jyh_loan\",\"token\":\""+token+"\"}";
		String res = sendPost(ConstantConfig.NOTEHTTPSMSURL,JsonStr);
		JSONObject jsonObj = JSONObject.parseObject(res.toString());
		JSONObject code = JSONObject.parseObject(jsonObj.get("data")+"");
		return code+"";
	}

	
	
	/**
	 * 向指定 URL 发送POST方法的请求  
	 * @param url 发送请求的 URL
	 * @param param 请求参数，请求参数应该是 name1=value1&name2=value2 的形式。
	 * @return 所代表远程资源的响应结果
	 */
	private static String sendPost( String url, String data ){
		String param = "data=" + data;
		PrintWriter	out	= null;
		BufferedReader	in	= null;
		String		result	= "";
		try {
			URL realUrl = new URL( url );
			URLConnection conn = realUrl.openConnection();
			conn.setRequestProperty( "accept", "*/*" );
			conn.setRequestProperty( "connection", "Keep-Alive" );
			conn.setRequestProperty( "user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)" );
			conn.setRequestProperty( "Accept-Charset", "utf-8" );
			conn.setRequestProperty( "contentType", "utf-8" );

			conn.setDoOutput( true );
			conn.setDoInput( true );
			out = new PrintWriter( conn.getOutputStream() );
			out.print( param );
			out.flush();
			in = new BufferedReader( new InputStreamReader( conn.getInputStream() ) );
			String line;
			while ( (line = in.readLine() ) != null )
			{
				result += line;
			}
		} catch ( Exception e ) {
			e.printStackTrace();
		}
		finally {
			try {
				if ( out != null ) {
					out.close();
				}
				if ( in != null ) {
					in.close();
				}
			} catch ( IOException ex ) {
				ex.printStackTrace();
			}
		}
		return(result);
	}	
}
